home *** CD-ROM | disk | FTP | other *** search
/ PCGUIA 127 / PC Guia 127.iso / Software / Produtividade / OpenOffice.org 2.0.1 / openofficeorg4.cab / test_optparse.py < prev    next >
Text File  |  2005-11-19  |  48KB  |  1,220 lines

  1. #!/usr/bin/python
  2.  
  3. #
  4. # Test suite for Optik.  Supplied by Johannes Gijsbers
  5. # (taradino@softhome.net) -- translated from the original Optik
  6. # test suite to this PyUnit-based version.
  7. #
  8. # $Id: test_optparse.py,v 1.2.8.2 2004/04/01 07:38:49 fdrake Exp $
  9. #
  10.  
  11. import sys
  12. import os
  13. import copy
  14. import unittest
  15.  
  16. from cStringIO import StringIO
  17. from pprint import pprint
  18. from test import test_support
  19.  
  20. from optparse import make_option, Option, IndentedHelpFormatter, \
  21.      TitledHelpFormatter, OptionParser, OptionContainer, OptionGroup, \
  22.      SUPPRESS_HELP, SUPPRESS_USAGE, OptionError, OptionConflictError, \
  23.      BadOptionError, OptionValueError
  24. from optparse import _match_abbrev
  25.  
  26. # Do the right thing with boolean values for all known Python versions.
  27. try:
  28.     True, False
  29. except NameError:
  30.     (True, False) = (1, 0)
  31.  
  32. class BaseTest(unittest.TestCase):
  33.     def assertParseOK(self, args, expected_opts, expected_positional_args):
  34.         """Assert the options are what we expected when parsing arguments.
  35.  
  36.         Otherwise, fail with a nicely formatted message.
  37.  
  38.         Keyword arguments:
  39.         args -- A list of arguments to parse with OptionParser.
  40.         expected_opts -- The options expected.
  41.         expected_positional_args -- The positional arguments expected.
  42.  
  43.         Returns the options and positional args for further testing.
  44.         """
  45.  
  46.         (options, positional_args) = self.parser.parse_args(args)
  47.         optdict = vars(options)
  48.  
  49.         self.assertEqual(optdict, expected_opts,
  50.                          """
  51. Options are %(optdict)s.
  52. Should be %(expected_opts)s.
  53. Args were %(args)s.""" % locals())
  54.  
  55.         self.assertEqual(positional_args, expected_positional_args,
  56.                          """
  57. Positional arguments are %(positional_args)s.
  58. Should be %(expected_positional_args)s.
  59. Args were %(args)s.""" % locals ())
  60.  
  61.         return (options, positional_args)
  62.  
  63.     def assertRaises(self, func, expected_exception, expected_output,
  64.                      get_output=None,
  65.                      funcargs=[], funckwargs={}):
  66.         """Assert the expected exception is raised when calling a function.
  67.  
  68.         Also check whether the right error message is given for a given error.
  69.  
  70.         Keyword arguments:
  71.         func -- The function to be called.
  72.         expected_exception -- The exception that should be raised.
  73.         expected_output -- The output we expect to see.
  74.         get_output -- The function to call to get the output.
  75.         funcargs -- The arguments `func` should be called with.
  76.         funckwargs -- The keyword arguments `func` should be called with.
  77.  
  78.         Returns the exception raised for further testing.
  79.         """
  80.         if get_output is None:
  81.             get_output = self.exception
  82.  
  83.         try:
  84.             out = func(*funcargs, **funckwargs)
  85.         except expected_exception, err:
  86.             output = get_output(err)
  87.  
  88.             self.failUnless(output.find(expected_output) != -1,
  89.                             """
  90. Message was:
  91. %(output)s
  92. Should contain:
  93. %(expected_output)s
  94. Function called:
  95. %(func)s
  96. With args/kwargs:
  97. %(funcargs)s/%(funckwargs)s""" % locals())
  98.  
  99.             return err
  100.         else:
  101.             self.fail("""
  102. No %(expected_exception)s raised.
  103. Function called:
  104. %(func)s
  105. With args/kwargs:
  106. %(funcargs)s/%(funckwargs)s""" % locals ())
  107.  
  108.     # -- Functions to be used as the get_output argument to assertRaises ------
  109.  
  110.     def exception(self, err):
  111.         return str(err)
  112.  
  113.     def redirected_stdout(self, err):
  114.         return sys.stdout.getvalue()
  115.  
  116.     # -- Assertions used in more than one class --------------------
  117.  
  118.     def assertParseFail(self, cmdline_args, expected_output):
  119.         """Assert the parser fails with the expected message."""
  120.         self.assertRaises(self.parser.parse_args, SystemExit, expected_output,
  121.                           funcargs=[cmdline_args])
  122.  
  123.     def assertStdoutEquals(self, cmdline_args, expected_output):
  124.         """Assert the parser prints the expected output on stdout."""
  125.         sys.stdout = StringIO()
  126.         self.assertRaises(self.parser.parse_args, SystemExit, expected_output,
  127.                           self.redirected_stdout, [cmdline_args])
  128.         sys.stdout = sys.__stdout__
  129.  
  130.     def assertTypeError(self, func, expected_output, *args):
  131.         """Assert a TypeError is raised when executing func."""
  132.         self.assertRaises(func, TypeError, expected_output, funcargs=args)
  133.  
  134. # -- Test make_option() aka Option -------------------------------------
  135.  
  136. # It's not necessary to test correct options here. All the tests in the
  137. # parser.parse_args() section deal with those, because they're needed
  138. # there. Duplication makes no sense to me.
  139.  
  140. class TestOptionChecks(BaseTest):
  141.     def setUp(self):
  142.         self.parser = OptionParser(usage=SUPPRESS_USAGE)
  143.  
  144.     def assertOptionError(self, expected_output, args=[], kwargs={}):
  145.         self.assertRaises(make_option, OptionError, expected_output,
  146.                           funcargs=args, funckwargs=kwargs)
  147.  
  148.     def test_opt_string_empty(self):
  149.         self.assertTypeError(make_option,
  150.                              "at least one option string must be supplied")
  151.  
  152.     def test_opt_string_too_short(self):
  153.         self.assertOptionError("invalid option string 'b': "
  154.                                "must be at least two characters long",
  155.                                ["b"])
  156.  
  157.     def test_opt_string_short_invalid(self):
  158.         self.assertOptionError("invalid short option string '--': must be "
  159.                                "of the form -x, (x any non-dash char)",
  160.                                ["--"])
  161.  
  162.     def test_opt_string_long_invalid(self):
  163.         self.assertOptionError("invalid long option string '---': "
  164.                                "must start with --, followed by non-dash",
  165.                                ["---"])
  166.  
  167.     def test_attr_invalid(self):
  168.         self.assertOptionError("invalid keyword arguments: foo, bar",
  169.                                ["-b"], {'foo': None, 'bar': None})
  170.  
  171.     def test_action_invalid(self):
  172.         self.assertOptionError("invalid action: 'foo'",
  173.                                ["-b"], {'action': 'foo'})
  174.  
  175.     def test_type_invalid(self):
  176.         self.assertOptionError("invalid option type: 'foo'",
  177.                                ["-b"], {'type': 'foo'})
  178.  
  179.     def test_no_type_for_action(self):
  180.         self.assertOptionError("must not supply a type for action 'count'",
  181.                                ["-b"], {'action': 'count', 'type': 'int'})
  182.  
  183.     def test_no_choices_list(self):
  184.         self.assertOptionError("must supply a list of "
  185.                                "choices for type 'choice'",
  186.                                ["-b", "--bad"], {'type': "choice"})
  187.  
  188.     def test_bad_choices_list(self):
  189.         typename = type('').__name__
  190.         self.assertOptionError("choices must be a list of "
  191.                                "strings ('%s' supplied)" % typename,
  192.                                ["-b", "--bad"],
  193.                                {'type': "choice", 'choices':"bad choices"})
  194.  
  195.     def test_no_choices_for_type(self):
  196.         self.assertOptionError("must not supply choices for type 'int'",
  197.                                ["-b"], {'type': 'int', 'choices':"bad"})
  198.  
  199.     def test_no_const_for_action(self):
  200.         self.assertOptionError("'const' must not be supplied for action "
  201.                                "'store'",
  202.                                ["-b"], {'action': 'store', 'const': 1})
  203.  
  204.     def test_no_nargs_for_action(self):
  205.         self.assertOptionError("'nargs' must not be supplied for action "
  206.                                "'count'",
  207.                                ["-b"], {'action': 'count', 'nargs': 2})
  208.  
  209.     def test_callback_not_callable(self):
  210.         self.assertOptionError("callback not callable: 'foo'",
  211.                                ["-b"], {'action': 'callback',
  212.                                         'callback': 'foo'})
  213.  
  214.     def dummy(self):
  215.         pass
  216.  
  217.     def test_callback_args_no_tuple(self):
  218.         self.assertOptionError("callback_args, if supplied, must be a tuple: "
  219.                                "not 'foo'",
  220.                                ["-b"], {'action': 'callback',
  221.                                         'callback': self.dummy,
  222.                                         'callback_args': 'foo'})
  223.  
  224.     def test_callback_kwargs_no_dict(self):
  225.         self.assertOptionError("callback_kwargs, if supplied, must be a dict: "
  226.                                "not 'foo'",
  227.                                ["-b"], {'action': 'callback',
  228.                                         'callback': self.dummy,
  229.                                         'callback_kwargs': 'foo'})
  230.  
  231.     def test_no_callback_for_action(self):
  232.         self.assertOptionError("callback supplied ('foo') for "
  233.                                "non-callback option",
  234.                                ["-b"], {'action': 'store',
  235.                                         'callback': 'foo'})
  236.  
  237.     def test_no_callback_args_for_action(self):
  238.         self.assertOptionError("callback_args supplied for non-callback "
  239.                                "option",
  240.                                ["-b"], {'action': 'store',
  241.                                         'callback_args': 'foo'})
  242.  
  243.     def test_no_callback_kwargs_for_action(self):
  244.         self.assertOptionError("callback_kwargs supplied for non-callback "
  245.                                "option",
  246.                                ["-b"], {'action': 'store',
  247.                                         'callback_kwargs': 'foo'})
  248.  
  249. class TestOptionParser(BaseTest):
  250.     def setUp(self):
  251.         self.parser = OptionParser()
  252.         self.parser.add_option("-v", "--verbose", "-n", "--noisy",
  253.                           action="store_true", dest="verbose")
  254.         self.parser.add_option("-q", "--quiet", "--silent",
  255.                           action="store_false", dest="verbose")
  256.  
  257.     def test_add_option_no_Option(self):
  258.         self.assertTypeError(self.parser.add_option,
  259.                              "not an Option instance: None", None)
  260.  
  261.     def test_add_option_invalid_arguments(self):
  262.         self.assertTypeError(self.parser.add_option,
  263.                              "invalid arguments", None, None)
  264.  
  265.     def test_get_option(self):
  266.         opt1 = self.parser.get_option("-v")
  267.         self.assert_(isinstance(opt1, Option))
  268.         self.assertEqual(opt1._short_opts, ["-v", "-n"])
  269.         self.assertEqual(opt1._long_opts, ["--verbose", "--noisy"])
  270.         self.assertEqual(opt1.action, "store_true")
  271.         self.assertEqual(opt1.dest, "verbose")
  272.  
  273.     def test_get_option_equals(self):
  274.         opt1 = self.parser.get_option("-v")
  275.         opt2 = self.parser.get_option("--verbose")
  276.         opt3 = self.parser.get_option("-n")
  277.         opt4 = self.parser.get_option("--noisy")
  278.         self.assert_(opt1 is opt2 is opt3 is opt4)
  279.  
  280.     def test_has_option(self):
  281.         self.assert_(self.parser.has_option("-v"))
  282.         self.assert_(self.parser.has_option("--verbose"))
  283.  
  284.     def assert_removed(self):
  285.         self.assert_(self.parser.get_option("-v") is None)
  286.         self.assert_(self.parser.get_option("--verbose") is None)
  287.         self.assert_(self.parser.get_option("-n") is None)
  288.         self.assert_(self.parser.get_option("--noisy") is None)
  289.  
  290.         self.failIf(self.parser.has_option("-v"))
  291.         self.failIf(self.parser.has_option("--verbose"))
  292.         self.failIf(self.parser.has_option("-n"))
  293.         self.failIf(self.parser.has_option("--noisy"))
  294.  
  295.         self.assert_(self.parser.has_option("-q"))
  296.         self.assert_(self.parser.has_option("--silent"))
  297.  
  298.     def test_remove_short_opt(self):
  299.         self.parser.remove_option("-n")
  300.         self.assert_removed()
  301.  
  302.     def test_remove_long_opt(self):
  303.         self.parser.remove_option("--verbose")
  304.         self.assert_removed()
  305.  
  306.     def test_remove_nonexistent(self):
  307.         self.assertRaises(self.parser.remove_option, ValueError,
  308.                           "no such option 'foo'", funcargs=['foo'])
  309.  
  310. # -- Test parser.parse_args() ------------------------------------------
  311.  
  312. class TestStandard(BaseTest):
  313.     def setUp(self):
  314.         options = [make_option("-a", type="string"),
  315.                    make_option("-b", "--boo", type="int", dest='boo'),
  316.                    make_option("--foo", action="append")]
  317.  
  318.         self.parser = OptionParser(usage=SUPPRESS_USAGE, option_list=options)
  319.  
  320.     def test_required_value(self):
  321.         self.assertParseFail(["-a"], "-a option requires a value")
  322.  
  323.     def test_invalid_integer(self):
  324.         self.assertParseFail(["-b", "5x"],
  325.                              "option -b: invalid integer value: '5x'")
  326.  
  327.     def test_no_such_option(self):
  328.         self.assertParseFail(["--boo13"], "no such option: --boo13")
  329.  
  330.     def test_long_invalid_integer(self):
  331.         self.assertParseFail(["--boo=x5"],
  332.                              "option --boo: invalid integer value: 'x5'")
  333.  
  334.     def test_empty(self):
  335.         self.assertParseOK([], {'a': None, 'boo': None, 'foo': None}, [])
  336.  
  337.     def test_shortopt_empty_longopt_append(self):
  338.         self.assertParseOK(["-a", "", "--foo=blah", "--foo="],
  339.                            {'a': "", 'boo': None, 'foo': ["blah", ""]},
  340.                            [])
  341.  
  342.     def test_long_option_append(self):
  343.         self.assertParseOK(["--foo", "bar", "--foo", "", "--foo=x"],
  344.                            {'a': None,
  345.                             'boo': None,
  346.                             'foo': ["bar", "", "x"]},
  347.                            [])
  348.  
  349.     def test_option_argument_joined(self):
  350.         self.assertParseOK(["-abc"],
  351.                            {'a': "bc", 'boo': None, 'foo': None},
  352.                            [])
  353.  
  354.     def test_option_argument_split(self):
  355.         self.assertParseOK(["-a", "34"],
  356.                            {'a': "34", 'boo': None, 'foo': None},
  357.                            [])
  358.  
  359.     def test_option_argument_joined_integer(self):
  360.         self.assertParseOK(["-b34"],
  361.                            {'a': None, 'boo': 34, 'foo': None},
  362.                            [])
  363.  
  364.     def test_option_argument_split_negative_integer(self):
  365.         self.assertParseOK(["-b", "-5"],
  366.                            {'a': None, 'boo': -5, 'foo': None},
  367.                            [])
  368.  
  369.     def test_long_option_argument_joined(self):
  370.         self.assertParseOK(["--boo=13"],
  371.                            {'a': None, 'boo': 13, 'foo': None},
  372.                            [])
  373.  
  374.     def test_long_option_argument_split(self):
  375.         self.assertParseOK(["--boo", "111"],
  376.                            {'a': None, 'boo': 111, 'foo': None},
  377.                            [])
  378.  
  379.     def test_long_option_short_option(self):
  380.         self.assertParseOK(["--foo=bar", "-axyz"],
  381.                            {'a': 'xyz', 'boo': None, 'foo': ["bar"]},
  382.                            [])
  383.  
  384.     def test_abbrev_long_option(self):
  385.         self.assertParseOK(["--f=bar", "-axyz"],
  386.                            {'a': 'xyz', 'boo': None, 'foo': ["bar"]},
  387.                            [])
  388.  
  389.     def test_defaults(self):
  390.         (options, args) = self.parser.parse_args([])
  391.         defaults = self.parser.get_default_values()
  392.         self.assertEqual(vars(defaults), vars(options))
  393.  
  394.     def test_ambiguous_option(self):
  395.         self.parser.add_option("--foz", action="store",
  396.                                type="string", dest="foo")
  397.         possibilities = ", ".join({"--foz": None, "--foo": None}.keys())
  398.         self.assertParseFail(["--f=bar"],
  399.                              "ambiguous option: --f (%s?)" % possibilities)
  400.  
  401.  
  402.     def test_short_and_long_option_split(self):
  403.         self.assertParseOK(["-a", "xyz", "--foo", "bar"],
  404.                            {'a': 'xyz', 'boo': None, 'foo': ["bar"]},
  405.                            []),
  406.  
  407.     def test_short_option_split_long_option_append(self):
  408.         self.assertParseOK(["--foo=bar", "-b", "123", "--foo", "baz"],
  409.                            {'a': None, 'boo': 123, 'foo': ["bar", "baz"]},
  410.                            [])
  411.  
  412.     def test_short_option_split_one_positional_arg(self):
  413.         self.assertParseOK(["-a", "foo", "bar"],
  414.                            {'a': "foo", 'boo': None, 'foo': None},
  415.                            ["bar"]),
  416.  
  417.     def test_short_option_consumes_separator(self):
  418.         self.assertParseOK(["-a", "--", "foo", "bar"],
  419.                            {'a': "--", 'boo': None, 'foo': None},
  420.                            ["foo", "bar"]),
  421.  
  422.     def test_short_option_joined_and_separator(self):
  423.         self.assertParseOK(["-ab", "--", "--foo", "bar"],
  424.                            {'a': "b", 'boo': None, 'foo': None},
  425.                            ["--foo", "bar"]),
  426.  
  427.     def test_invalid_option_becomes_positional_arg(self):
  428.         self.assertParseOK(["-ab", "-", "--foo", "bar"],
  429.                            {'a': "b", 'boo': None, 'foo': ["bar"]},
  430.                            ["-"])
  431.  
  432.     def test_no_append_versus_append(self):
  433.         self.assertParseOK(["-b3", "-b", "5", "--foo=bar", "--foo", "baz"],
  434.                            {'a': None, 'boo': 5, 'foo': ["bar", "baz"]},
  435.                            [])
  436.  
  437.     def test_option_consumes_optionlike_string(self):
  438.         self.assertParseOK(["-a", "-b3"],
  439.                            {'a': "-b3", 'boo': None, 'foo': None},
  440.                            [])
  441.  
  442. class TestBool(BaseTest):
  443.     def setUp(self):
  444.         options = [make_option("-v",
  445.                                "--verbose",
  446.                                action="store_true",
  447.                                dest="verbose",
  448.                                default=''),
  449.                    make_option("-q",
  450.                                "--quiet",
  451.                                action="store_false",
  452.                                dest="verbose")]
  453.         self.parser = OptionParser(option_list = options)
  454.  
  455.     def test_bool_default(self):
  456.         self.assertParseOK([],
  457.                            {'verbose': ''},
  458.                            [])
  459.  
  460.     def test_bool_false(self):
  461.         (options, args) = self.assertParseOK(["-q"],
  462.                                              {'verbose': 0},
  463.                                              [])
  464.         if hasattr(__builtins__, 'False'):
  465.             self.failUnless(options.verbose is False)
  466.  
  467.     def test_bool_true(self):
  468.         (options, args) = self.assertParseOK(["-v"],
  469.                                              {'verbose': 1},
  470.                                              [])
  471.         if hasattr(__builtins__, 'True'):
  472.             self.failUnless(options.verbose is True)
  473.  
  474.     def test_bool_flicker_on_and_off(self):
  475.         self.assertParseOK(["-qvq", "-q", "-v"],
  476.                            {'verbose': 1},
  477.                            [])
  478.  
  479. class TestChoice(BaseTest):
  480.     def setUp(self):
  481.         self.parser = OptionParser(usage=SUPPRESS_USAGE)
  482.         self.parser.add_option("-c", action="store", type="choice",
  483.                                dest="choice", choices=["one", "two", "three"])
  484.  
  485.     def test_valid_choice(self):
  486.         self.assertParseOK(["-c", "one", "xyz"],
  487.                            {'choice': 'one'},
  488.                            ["xyz"])
  489.  
  490.     def test_invalid_choice(self):
  491.         self.assertParseFail(["-c", "four", "abc"],
  492.                              "option -c: invalid choice: 'four' "
  493.                              "(choose from 'one', 'two', 'three')")
  494.  
  495.     def test_add_choice_option(self):
  496.         self.parser.add_option("-d", "--default",
  497.                                choices=["four", "five", "six"])
  498.         opt = self.parser.get_option("-d")
  499.         self.assertEqual(opt.type, "choice")
  500.         self.assertEqual(opt.action, "store")
  501.  
  502. class TestCount(BaseTest):
  503.     def setUp(self):
  504.         self.parser = OptionParser(usage=SUPPRESS_USAGE)
  505.         self.v_opt = make_option("-v", action="count", dest="verbose")
  506.         self.parser.add_option(self.v_opt)
  507.         self.parser.add_option("--verbose", type="int", dest="verbose")
  508.         self.parser.add_option("-q", "--quiet",
  509.                                action="store_const", dest="verbose", const=0)
  510.  
  511.     def test_empty(self):
  512.         self.assertParseOK([], {'verbose': None}, [])
  513.  
  514.     def test_count_one(self):
  515.         self.assertParseOK(["-v"], {'verbose': 1}, [])
  516.  
  517.     def test_count_three(self):
  518.         self.assertParseOK(["-vvv"], {'verbose': 3}, [])
  519.  
  520.     def test_count_three_apart(self):
  521.         self.assertParseOK(["-v", "-v", "-v"], {'verbose': 3}, [])
  522.  
  523.     def test_count_override_amount(self):
  524.         self.assertParseOK(["-vvv", "--verbose=2"], {'verbose': 2}, [])
  525.  
  526.     def test_count_override_quiet(self):
  527.         self.assertParseOK(["-vvv", "--verbose=2", "-q"], {'verbose': 0}, [])
  528.  
  529.     def test_count_overriding(self):
  530.         self.assertParseOK(["-vvv", "--verbose=2", "-q", "-v"],
  531.                            {'verbose': 1}, [])
  532.  
  533.     def test_count_interspersed_args(self):
  534.         self.assertParseOK(["--quiet", "3", "-v"],
  535.                            {'verbose': 1},
  536.                            ["3"])
  537.  
  538.     def test_count_no_interspersed_args(self):
  539.         self.parser.disable_interspersed_args()
  540.         self.assertParseOK(["--quiet", "3", "-v"],
  541.                            {'verbose': 0},
  542.                            ["3", "-v"])
  543.  
  544.     def test_count_no_such_option(self):
  545.         self.assertParseFail(["-q3", "-v"], "no such option: -3")
  546.  
  547.     def test_count_option_no_value(self):
  548.         self.assertParseFail(["--quiet=3", "-v"],
  549.                              "--quiet option does not take a value")
  550.  
  551.     def test_count_with_default(self):
  552.         self.parser.set_default('verbose', 0)
  553.         self.assertParseOK([], {'verbose':0}, [])
  554.  
  555.     def test_count_overriding_default(self):
  556.         self.parser.set_default('verbose', 0)
  557.         self.assertParseOK(["-vvv", "--verbose=2", "-q", "-v"],
  558.                            {'verbose': 1}, [])
  559.  
  560. class TestNArgs(BaseTest):
  561.     def setUp(self):
  562.         self.parser = OptionParser(usage=SUPPRESS_USAGE)
  563.         self.parser.add_option("-p", "--point",
  564.                                action="store", nargs=3, type="float", dest="point")
  565.  
  566.     def test_nargs_with_positional_args(self):
  567.         self.assertParseOK(["foo", "-p", "1", "2.5", "-4.3", "xyz"],
  568.                            {'point': (1.0, 2.5, -4.3)},
  569.                            ["foo", "xyz"])
  570.  
  571.     def test_nargs_long_opt(self):
  572.         self.assertParseOK(["--point", "-1", "2.5", "-0", "xyz"],
  573.                            {'point': (-1.0, 2.5, -0.0)},
  574.                            ["xyz"])
  575.  
  576.     def test_nargs_invalid_float_value(self):
  577.         self.assertParseFail(["-p", "1.0", "2x", "3.5"],
  578.                              "option -p: "
  579.                              "invalid floating-point value: '2x'")
  580.  
  581.     def test_nargs_required_values(self):
  582.         self.assertParseFail(["--point", "1.0", "3.5"],
  583.                              "--point option requires 3 values")
  584.  
  585. class TestNArgsAppend(BaseTest):
  586.     def setUp(self):
  587.         self.parser = OptionParser(usage=SUPPRESS_USAGE)
  588.         self.parser.add_option("-p", "--point", action="store", nargs=3,
  589.                                type="float", dest="point")
  590.         self.parser.add_option("-f", "--foo", action="append", nargs=2,
  591.                                type="int", dest="foo")
  592.  
  593.     def test_nargs_append(self):
  594.         self.assertParseOK(["-f", "4", "-3", "blah", "--foo", "1", "666"],
  595.                            {'point': None, 'foo': [(4, -3), (1, 666)]},
  596.                            ["blah"])
  597.  
  598.     def test_nargs_append_required_values(self):
  599.         self.assertParseFail(["-f4,3"],
  600.                              "-f option requires 2 values")
  601.  
  602.     def test_nargs_append_simple(self):
  603.         self.assertParseOK(["--foo=3", "4"],
  604.                            {'point': None, 'foo':[(3, 4)]},
  605.                            [])
  606.  
  607. class TestVersion(BaseTest):
  608.     def test_version(self):
  609.         oldargv = sys.argv[0]
  610.         sys.argv[0] = os.path.join(os.curdir, "foo", "bar")
  611.         self.parser = OptionParser(usage=SUPPRESS_USAGE, version="%prog 0.1")
  612.         self.assertStdoutEquals(["--version"], "bar 0.1\n")
  613.         sys.argv[0] = oldargv
  614.  
  615.     def test_version_with_prog_keyword(self):
  616.         oldargv = sys.argv[0]
  617.         sys.argv[0] = "./foo/bar"
  618.         self.parser = OptionParser(usage=SUPPRESS_USAGE, version="%prog 0.1",
  619.                                    prog="splat")
  620.         self.assertStdoutEquals(["--version"], "splat 0.1\n")
  621.         sys.argv[0] = oldargv
  622.  
  623.     def test_version_with_prog_attribute(self):
  624.         oldargv = sys.argv[0]
  625.         sys.argv[0] = "./foo/bar"
  626.         self.parser = OptionParser(usage=SUPPRESS_USAGE, version="%prog 0.1")
  627.         self.parser.prog = "splat"
  628.         self.assertStdoutEquals(["--version"], "splat 0.1\n")
  629.         sys.argv[0] = oldargv
  630.  
  631.     def test_no_version(self):
  632.         self.parser = OptionParser(usage=SUPPRESS_USAGE)
  633.         self.assertParseFail(["--version"],
  634.                              "no such option: --version")
  635.  
  636. # -- Test conflicting default values and parser.parse_args() -----------
  637.  
  638. class TestConflictingDefaults(BaseTest):
  639.     """Conflicting default values: the last one should win."""
  640.     def setUp(self):
  641.         self.parser = OptionParser(option_list=[
  642.             make_option("-v", action="store_true", dest="verbose", default=1)])
  643.  
  644.     def test_conflict_default(self):
  645.         self.parser.add_option("-q", action="store_false", dest="verbose",
  646.                                default=0)
  647.         self.assertParseOK([], {'verbose': 0}, [])
  648.  
  649.     def test_conflict_default_none(self):
  650.         self.parser.add_option("-q", action="store_false", dest="verbose",
  651.                                default=None)
  652.         self.assertParseOK([], {'verbose': None}, [])
  653.  
  654. class TestOptionGroup(BaseTest):
  655.     def setUp(self):
  656.         self.parser = OptionParser(usage=SUPPRESS_USAGE)
  657.  
  658.     def test_option_group_create_instance(self):
  659.         group = OptionGroup(self.parser, "Spam")
  660.         self.parser.add_option_group(group)
  661.         group.add_option("--spam", action="store_true",
  662.                          help="spam spam spam spam")
  663.         self.assertParseOK(["--spam"], {'spam': 1}, [])
  664.  
  665.     def test_add_group_no_group(self):
  666.         self.assertTypeError(self.parser.add_option_group,
  667.                              "not an OptionGroup instance: None", None)
  668.  
  669.     def test_add_group_invalid_arguments(self):
  670.         self.assertTypeError(self.parser.add_option_group,
  671.                              "invalid arguments", None, None)
  672.  
  673.     def test_add_group_wrong_parser(self):
  674.         group = OptionGroup(self.parser, "Spam")
  675.         group.parser = OptionParser()
  676.         self.assertRaises(self.parser.add_option_group, ValueError,
  677.                           "invalid OptionGroup (wrong parser)", funcargs=[group])
  678.  
  679.     def test_group_manipulate(self):
  680.         group = self.parser.add_option_group("Group 2",
  681.                                              description="Some more options")
  682.         group.set_title("Bacon")
  683.         group.add_option("--bacon", type="int")
  684.         self.assert_(self.parser.get_option_group("--bacon"), group)
  685.  
  686. # -- Test extending and parser.parse_args() ----------------------------
  687.  
  688. class TestExtendAddTypes(BaseTest):
  689.     def setUp(self):
  690.         self.parser = OptionParser(usage=SUPPRESS_USAGE,
  691.                                    option_class=self.MyOption)
  692.         self.parser.add_option("-a", None, type="string", dest="a")
  693.         self.parser.add_option("-f", "--file", type="file", dest="file")
  694.  
  695.     class MyOption (Option):
  696.         def check_file (option, opt, value):
  697.             if not os.path.exists(value):
  698.                 raise OptionValueError("%s: file does not exist" % value)
  699.             elif not os.path.isfile(value):
  700.                 raise OptionValueError("%s: not a regular file" % value)
  701.             return value
  702.  
  703.         TYPES = Option.TYPES + ("file",)
  704.         TYPE_CHECKER = copy.copy(Option.TYPE_CHECKER)
  705.         TYPE_CHECKER["file"] = check_file
  706.  
  707.     def test_extend_file(self):
  708.         open(test_support.TESTFN, "w").close()
  709.         self.assertParseOK(["--file", test_support.TESTFN, "-afoo"],
  710.                            {'file': test_support.TESTFN, 'a': 'foo'},
  711.                            [])
  712.  
  713.         os.unlink(test_support.TESTFN)
  714.  
  715.     def test_extend_file_nonexistent(self):
  716.         self.assertParseFail(["--file", test_support.TESTFN, "-afoo"],
  717.                              "%s: file does not exist" %
  718.                              test_support.TESTFN)
  719.  
  720.     def test_file_irregular(self):
  721.         os.mkdir(test_support.TESTFN)
  722.         self.assertParseFail(["--file", test_support.TESTFN, "-afoo"],
  723.                              "%s: not a regular file" %
  724.                              test_support.TESTFN)
  725.         os.rmdir(test_support.TESTFN)
  726.  
  727. class TestExtendAddActions(BaseTest):
  728.     def setUp(self):
  729.         options = [self.MyOption("-a", "--apple", action="extend",
  730.                                  type="string", dest="apple")]
  731.         self.parser = OptionParser(option_list=options)
  732.  
  733.     class MyOption (Option):
  734.         ACTIONS = Option.ACTIONS + ("extend",)
  735.         STORE_ACTIONS = Option.STORE_ACTIONS + ("extend",)
  736.         TYPED_ACTIONS = Option.TYPED_ACTIONS + ("extend",)
  737.  
  738.         def take_action (self, action, dest, opt, value, values, parser):
  739.             if action == "extend":
  740.                 lvalue = value.split(",")
  741.                 values.ensure_value(dest, []).extend(lvalue)
  742.             else:
  743.                 Option.take_action(self, action, dest, opt, parser, value,
  744.                                    values)
  745.  
  746.     def test_extend_add_action(self):
  747.         self.assertParseOK(["-afoo,bar", "--apple=blah"],
  748.                            {'apple': ["foo", "bar", "blah"]},
  749.                            [])
  750.  
  751.     def test_extend_add_action_normal(self):
  752.         self.assertParseOK(["-a", "foo", "-abar", "--apple=x,y"],
  753.                            {'apple': ["foo", "bar", "x", "y"]},
  754.                            [])
  755.  
  756. # -- Test callbacks and parser.parse_args() ----------------------------
  757.  
  758. class TestCallback(BaseTest):
  759.     def setUp(self):
  760.         options = [make_option("-x",
  761.                                None,
  762.                                action="callback",
  763.                                callback=self.process_opt),
  764.                    make_option("-f",
  765.                                "--file",
  766.                                action="callback",
  767.                                callback=self.process_opt,
  768.                                type="string",
  769.                                dest="filename")]
  770.         self.parser = OptionParser(option_list=options)
  771.  
  772.     def process_opt(self, option, opt, value, parser_):
  773.         if opt == "-x":
  774.             self.assertEqual(option._short_opts, ["-x"])
  775.             self.assertEqual(option._long_opts, [])
  776.             self.assert_(parser_ is self.parser)
  777.             self.assert_(value is None)
  778.             self.assertEqual(vars(parser_.values), {'filename': None})
  779.  
  780.             parser_.values.x = 42
  781.         elif opt == "--file":
  782.             self.assertEqual(option._short_opts, ["-f"])
  783.             self.assertEqual(option._long_opts, ["--file"])
  784.             self.assert_(parser_ is self.parser)
  785.             self.assertEqual(value, "foo")
  786.             self.assertEqual(vars(parser_.values), {'filename': None, 'x': 42})
  787.  
  788.             setattr(parser_.values, option.dest, value)
  789.         else:
  790.             self.fail("Unknown option %r in process_opt." % opt)
  791.  
  792.     def test_callback(self):
  793.         self.assertParseOK(["-x", "--file=foo"],
  794.                            {'filename': "foo", 'x': 42},
  795.                            [])
  796.  
  797. class TestCallBackExtraArgs(BaseTest):
  798.     def setUp(self):
  799.         options = [make_option("-p", "--point", action="callback",
  800.                                callback=self.process_tuple,
  801.                                callback_args=(3, int), type="string",
  802.                                dest="points", default=[])]
  803.         self.parser = OptionParser(option_list=options)
  804.  
  805.     def process_tuple (self, option, opt, value, parser_, len, type):
  806.         self.assertEqual(len, 3)
  807.         self.assert_(type is int)
  808.  
  809.         if opt == "-p":
  810.             self.assertEqual(value, "1,2,3")
  811.         elif opt == "--point":
  812.             self.assertEqual(value, "4,5,6")
  813.  
  814.         value = tuple(map(type, value.split(",")))
  815.         getattr(parser_.values, option.dest).append(value)
  816.  
  817.     def test_callback_extra_args(self):
  818.         self.assertParseOK(["-p1,2,3", "--point", "4,5,6"],
  819.                            {'points': [(1,2,3), (4,5,6)]},
  820.                            [])
  821.  
  822. class TestCallBackMeddleArgs(BaseTest):
  823.     def setUp(self):
  824.         options = [make_option(str(x), action="callback",
  825.                                callback=self.process_n, dest='things')
  826.                    for x in range(-1, -6, -1)]
  827.         self.parser = OptionParser(option_list=options)
  828.  
  829.     # Callback that meddles in rargs, largs
  830.     def process_n (self, option, opt, value, parser_):
  831.         # option is -3, -5, etc.
  832.         nargs = int(opt[1:])
  833.         rargs = parser_.rargs
  834.         if len(rargs) < nargs:
  835.             self.fail("Expected %d arguments for %s option." % (nargs, opt))
  836.         dest = parser_.values.ensure_value(option.dest, [])
  837.         dest.append(tuple(rargs[0:nargs]))
  838.         parser_.largs.append(nargs)
  839.         del rargs[0:nargs]
  840.  
  841.     def test_callback_meddle_args(self):
  842.         self.assertParseOK(["-1", "foo", "-3", "bar", "baz", "qux"],
  843.                            {'things': [("foo",), ("bar", "baz", "qux")]},
  844.                            [1, 3])
  845.  
  846.     def test_callback_meddle_args_separator(self):
  847.         self.assertParseOK(["-2", "foo", "--"],
  848.                            {'things': [('foo', '--')]},
  849.                            [2])
  850.  
  851. class TestCallBackManyArgs(BaseTest):
  852.     def setUp(self):
  853.         options = [make_option("-a", "--apple", action="callback", nargs=2,
  854.                                callback=self.process_many, type="string"),
  855.                    make_option("-b", "--bob", action="callback", nargs=3,
  856.                                callback=self.process_many, type="int")]
  857.         self.parser = OptionParser(option_list=options)
  858.  
  859.     def process_many (self, option, opt, value, parser_):
  860.         if opt == "-a":
  861.             self.assertEqual(value, ("foo", "bar"))
  862.         elif opt == "--apple":
  863.             self.assertEqual(value, ("ding", "dong"))
  864.         elif opt == "-b":
  865.             self.assertEqual(value, (1, 2, 3))
  866.         elif opt == "--bob":
  867.             self.assertEqual(value, (-666, 42, 0))
  868.  
  869.     def test_many_args(self):
  870.         self.assertParseOK(["-a", "foo", "bar", "--apple", "ding", "dong",
  871.                             "-b", "1", "2", "3", "--bob", "-666", "42",
  872.                             "0"],
  873.                            {},
  874.                            [])
  875.  
  876. class TestCallBackCheckAbbrev(BaseTest):
  877.     def setUp(self):
  878.         self.parser = OptionParser()
  879.         self.parser.add_option("--foo-bar", action="callback",
  880.                                callback=self.check_abbrev)
  881.  
  882.     def check_abbrev (self, option, opt, value, parser):
  883.         self.assertEqual(opt, "--foo-bar")
  884.  
  885.     def test_abbrev_callback_expansion(self):
  886.         self.assertParseOK(["--foo"], {}, [])
  887.  
  888. class TestCallBackVarArgs(BaseTest):
  889.     def setUp(self):
  890.         options = [make_option("-a", type="int", nargs=2, dest="a"),
  891.                    make_option("-b", action="store_true", dest="b"),
  892.                    make_option("-c", "--callback", action="callback",
  893.                                callback=self.variable_args, dest="c")]
  894.         self.parser = OptionParser(usage=SUPPRESS_USAGE, option_list=options)
  895.  
  896.     def variable_args (self, option, opt, value, parser):
  897.         self.assert_(value is None)
  898.         done = 0
  899.         value = []
  900.         rargs = parser.rargs
  901.         while rargs:
  902.             arg = rargs[0]
  903.             if ((arg[:2] == "--" and len(arg) > 2) or
  904.                 (arg[:1] == "-" and len(arg) > 1 and arg[1] != "-")):
  905.                 break
  906.             else:
  907.                 value.append(arg)
  908.                 del rargs[0]
  909.         setattr(parser.values, option.dest, value)
  910.  
  911.     def test_variable_args(self):
  912.         self.assertParseOK(["-a3", "-5", "--callback", "foo", "bar"],
  913.                            {'a': (3, -5), 'b': None, 'c': ["foo", "bar"]},
  914.                            [])
  915.  
  916.     def test_consume_separator_stop_at_option(self):
  917.         self.assertParseOK(["-c", "37", "--", "xxx", "-b", "hello"],
  918.                            {'a': None,
  919.                             'b': True,
  920.                             'c': ["37", "--", "xxx"]},
  921.                            ["hello"])
  922.  
  923.     def test_positional_arg_and_variable_args(self):
  924.         self.assertParseOK(["hello", "-c", "foo", "-", "bar"],
  925.                            {'a': None,
  926.                             'b': None,
  927.                             'c':["foo", "-", "bar"]},
  928.                            ["hello"])
  929.  
  930.     def test_stop_at_option(self):
  931.         self.assertParseOK(["-c", "foo", "-b"],
  932.                            {'a': None, 'b': True, 'c': ["foo"]},
  933.                            [])
  934.  
  935.     def test_stop_at_invalid_option(self):
  936.         self.assertParseFail(["-c", "3", "-5", "-a"], "no such option: -5")
  937.  
  938.  
  939. # -- Test conflict handling and parser.parse_args() --------------------
  940.  
  941. class ConflictBase(BaseTest):
  942.     def setUp(self):
  943.         options = [make_option("-v", "--verbose", action="count",
  944.                                dest="verbose", help="increment verbosity")]
  945.         self.parser = OptionParser(usage=SUPPRESS_USAGE, option_list=options)
  946.  
  947.     def show_version (self, option, opt, value, parser):
  948.         parser.values.show_version = 1
  949.  
  950. class TestConflict(ConflictBase):
  951.     """Use the default conflict resolution for Optik 1.2: error."""
  952.     def assert_conflict_error(self, func):
  953.         err = self.assertRaises(func, OptionConflictError,
  954.                                 "option -v/--version: conflicting option "
  955.                                 "string(s): -v",
  956.                                 funcargs=["-v", "--version"],
  957.                                 funckwargs={'action':"callback",
  958.                                             'callback':self.show_version,
  959.                                             'help':"show version"})
  960.  
  961.         self.assertEqual(err.msg, "conflicting option string(s): -v")
  962.         self.assertEqual(err.option_id, "-v/--version")
  963.  
  964.     def test_conflict_error(self):
  965.         self.assert_conflict_error(self.parser.add_option)
  966.  
  967.     def test_conflict_error_group(self):
  968.         group = OptionGroup(self.parser, "Group 1")
  969.         self.assert_conflict_error(group.add_option)
  970.  
  971.     def test_no_such_conflict_handler(self):
  972.         self.assertRaises(self.parser.set_conflict_handler, ValueError,
  973.                           "invalid conflict_resolution value 'foo'",
  974.                           funcargs=['foo'])
  975.  
  976.  
  977. class TestConflictIgnore(ConflictBase):
  978.     """Test the old (Optik <= 1.1 behaviour) -- arguably broken, but
  979.     still available so should be tested.
  980.     """
  981.  
  982.     def setUp(self):
  983.         ConflictBase.setUp(self)
  984.         self.parser.set_conflict_handler("ignore")
  985.         self.parser.add_option("-v", "--version", action="callback",
  986.                           callback=self.show_version, help="show version")
  987.  
  988.     def test_conflict_ignore(self):
  989.         v_opt = self.parser.get_option("-v")
  990.         verbose_opt = self.parser.get_option("--verbose")
  991.         version_opt = self.parser.get_option("--version")
  992.  
  993.         self.assert_(v_opt is version_opt)
  994.         self.assert_(v_opt is not verbose_opt)
  995.         self.assertEqual(v_opt._long_opts, ["--version"])
  996.         self.assertEqual(version_opt._short_opts, ["-v"])
  997.         self.assertEqual(verbose_opt._short_opts, ["-v"])
  998.  
  999.     def test_conflict_ignore_help(self):
  1000.         self.assertStdoutEquals(["-h"], """\
  1001. options:
  1002.   -v, --verbose  increment verbosity
  1003.   -h, --help     show this help message and exit
  1004.   -v, --version  show version
  1005. """)
  1006.  
  1007.     def test_conflict_ignore_short_opt(self):
  1008.         self.assertParseOK(["-v"],
  1009.                            {'show_version': 1, 'verbose': None},
  1010.                            [])
  1011.  
  1012. class TestConflictResolve(ConflictBase):
  1013.     def setUp(self):
  1014.         ConflictBase.setUp(self)
  1015.         self.parser.set_conflict_handler("resolve")
  1016.         self.parser.add_option("-v", "--version", action="callback",
  1017.                                callback=self.show_version, help="show version")
  1018.  
  1019.     def test_conflict_resolve(self):
  1020.         v_opt = self.parser.get_option("-v")
  1021.         verbose_opt = self.parser.get_option("--verbose")
  1022.         version_opt = self.parser.get_option("--version")
  1023.  
  1024.         self.assert_(v_opt is version_opt)
  1025.         self.assert_(v_opt is not verbose_opt)
  1026.         self.assertEqual(v_opt._long_opts, ["--version"])
  1027.         self.assertEqual(version_opt._short_opts, ["-v"])
  1028.         self.assertEqual(version_opt._long_opts, ["--version"])
  1029.         self.assertEqual(verbose_opt._short_opts, [])
  1030.         self.assertEqual(verbose_opt._long_opts, ["--verbose"])
  1031.  
  1032.     def test_conflict_resolve_help(self):
  1033.         self.assertStdoutEquals(["-h"], """\
  1034. options:
  1035.   --verbose      increment verbosity
  1036.   -h, --help     show this help message and exit
  1037.   -v, --version  show version
  1038. """)
  1039.  
  1040.     def test_conflict_resolve_short_opt(self):
  1041.         self.assertParseOK(["-v"],
  1042.                            {'verbose': None, 'show_version': 1},
  1043.                            [])
  1044.  
  1045.     def test_conflict_resolve_long_opt(self):
  1046.         self.assertParseOK(["--verbose"],
  1047.                            {'verbose': 1},
  1048.                            [])
  1049.  
  1050.     def test_conflict_resolve_long_opts(self):
  1051.         self.assertParseOK(["--verbose", "--version"],
  1052.                            {'verbose': 1, 'show_version': 1},
  1053.                            [])
  1054.  
  1055. class TestConflictOverride(BaseTest):
  1056.     def setUp(self):
  1057.         self.parser = OptionParser(usage=SUPPRESS_USAGE)
  1058.         self.parser.set_conflict_handler("resolve")
  1059.         self.parser.add_option("-n", "--dry-run",
  1060.                                action="store_true", dest="dry_run",
  1061.                                help="don't do anything")
  1062.         self.parser.add_option("--dry-run", "-n",
  1063.                                action="store_const", const=42, dest="dry_run",
  1064.                                help="dry run mode")
  1065.  
  1066.     def test_conflict_override_opts(self):
  1067.         opt = self.parser.get_option("--dry-run")
  1068.         self.assertEqual(opt._short_opts, ["-n"])
  1069.         self.assertEqual(opt._long_opts, ["--dry-run"])
  1070.  
  1071.     def test_conflict_override_help(self):
  1072.         self.assertStdoutEquals(["-h"], """\
  1073. options:
  1074.   -h, --help     show this help message and exit
  1075.   -n, --dry-run  dry run mode
  1076. """)
  1077.  
  1078.     def test_conflict_override_args(self):
  1079.         self.assertParseOK(["-n"],
  1080.                            {'dry_run': 42},
  1081.                            [])
  1082.  
  1083. # -- Other testing. ----------------------------------------------------
  1084.  
  1085. class TestHelp(BaseTest):
  1086.     def setUp(self):
  1087.         options = [
  1088.             make_option("-a", type="string", dest='a',
  1089.                         metavar="APPLE", help="throw APPLEs at basket"),
  1090.             make_option("-b", "--boo", type="int", dest='boo',
  1091.                         metavar="NUM",
  1092.                         help=
  1093.                         "shout \"boo!\" NUM times (in order to frighten away "
  1094.                         "all the evil spirits that cause trouble and mayhem)"),
  1095.             make_option("--foo", action="append", type="string", dest='foo',
  1096.                         help="store FOO in the foo list for later fooing"),
  1097.             ]
  1098.  
  1099.         usage = "%prog [options]"
  1100.         self.parser = OptionParser(usage=usage, option_list=options)
  1101.  
  1102.     def assertHelpEquals(self, expected_output):
  1103.         # This trick is used to make optparse believe bar.py is being executed.
  1104.         oldargv = sys.argv[0]
  1105.         sys.argv[0] = os.path.join(os.curdir, "foo", "bar.py")
  1106.  
  1107.         self.assertStdoutEquals(["-h"], expected_output)
  1108.  
  1109.         sys.argv[0] = oldargv
  1110.  
  1111.     def test_help(self):
  1112.         self.assertHelpEquals("""\
  1113. usage: bar.py [options]
  1114.  
  1115. options:
  1116.   -aAPPLE           throw APPLEs at basket
  1117.   -bNUM, --boo=NUM  shout "boo!" NUM times (in order to frighten away all
  1118.                     the evil spirits that cause trouble and mayhem)
  1119.   --foo=FOO         store FOO in the foo list for later fooing
  1120.   -h, --help        show this help message and exit
  1121. """)
  1122.  
  1123.     def test_help_old_usage(self):
  1124.         self.parser.set_usage("usage: %prog [options]")
  1125.         self.assertHelpEquals("""\
  1126. usage: bar.py [options]
  1127.  
  1128. options:
  1129.   -aAPPLE           throw APPLEs at basket
  1130.   -bNUM, --boo=NUM  shout "boo!" NUM times (in order to frighten away all
  1131.                     the evil spirits that cause trouble and mayhem)
  1132.   --foo=FOO         store FOO in the foo list for later fooing
  1133.   -h, --help        show this help message and exit
  1134. """)
  1135.  
  1136.     def test_help_long_opts_first(self):
  1137.         self.parser.formatter.short_first = 0
  1138.         self.assertHelpEquals("""\
  1139. usage: bar.py [options]
  1140.  
  1141. options:
  1142.   -aAPPLE           throw APPLEs at basket
  1143.   --boo=NUM, -bNUM  shout "boo!" NUM times (in order to frighten away all
  1144.                     the evil spirits that cause trouble and mayhem)
  1145.   --foo=FOO         store FOO in the foo list for later fooing
  1146.   --help, -h        show this help message and exit
  1147. """)
  1148.  
  1149.     def test_help_title_formatter(self):
  1150.         self.parser.formatter = TitledHelpFormatter()
  1151.         self.assertHelpEquals("""\
  1152. Usage
  1153. =====
  1154.   bar.py [options]
  1155.  
  1156. options
  1157. =======
  1158. -aAPPLE           throw APPLEs at basket
  1159. --boo=NUM, -bNUM  shout "boo!" NUM times (in order to frighten away all
  1160.                   the evil spirits that cause trouble and mayhem)
  1161. --foo=FOO         store FOO in the foo list for later fooing
  1162. --help, -h        show this help message and exit
  1163. """)
  1164.  
  1165.     def test_help_description_groups(self):
  1166.         self.parser.set_description(
  1167.             "This is the program description.  This program has "
  1168.             "an option group as well as single options.")
  1169.  
  1170.         group = OptionGroup(
  1171.             self.parser, "Dangerous Options",
  1172.             "Caution: use of these options is at your own risk.  "
  1173.             "It is believed that some of them bite.")
  1174.         group.add_option("-g", action="store_true", help="Group option.")
  1175.         self.parser.add_option_group(group)
  1176.  
  1177.         self.assertHelpEquals("""\
  1178. usage: bar.py [options]
  1179.  
  1180. This is the program description.  This program has an option group as well as
  1181. single options.
  1182. options:
  1183.   -aAPPLE           throw APPLEs at basket
  1184.   -bNUM, --boo=NUM  shout "boo!" NUM times (in order to frighten away all
  1185.                     the evil spirits that cause trouble and mayhem)
  1186.   --foo=FOO         store FOO in the foo list for later fooing
  1187.   -h, --help        show this help message and exit
  1188.  
  1189.   Dangerous Options:
  1190.     Caution: use of these options is at your own risk.  It is believed that
  1191.     some of them bite.
  1192.     -g              Group option.
  1193. """)
  1194.  
  1195. class TestMatchAbbrev(BaseTest):
  1196.     def test_match_abbrev(self):
  1197.         self.assertEqual(_match_abbrev("--f",
  1198.                                        {"--foz": None,
  1199.                                         "--foo": None,
  1200.                                         "--fie": None,
  1201.                                         "--f": None}),
  1202.                          "--f")
  1203.  
  1204.     def test_match_abbrev_error(self):
  1205.         s = "--f"
  1206.         wordmap = {"--foz": None, "--foo": None, "--fie": None}
  1207.         possibilities = ", ".join(wordmap.keys())
  1208.         self.assertRaises(_match_abbrev, BadOptionError,
  1209.                           "ambiguous option: --f (%s?)" % possibilities,
  1210.                           funcargs=[s, wordmap])
  1211.  
  1212. def test_main():
  1213.     mod = sys.modules[__name__]
  1214.     test_support.run_unittest(
  1215.         *[getattr(mod, name) for name in dir(mod) if name.startswith('Test')]
  1216.     )
  1217.  
  1218. if __name__ == '__main__':
  1219.     unittest.main()
  1220.